import {
    HTTP
} from "../util/http"
class ClassicModel extends HTTP {
    getLatest(sCallback) {
        this.request({
            url: "classic/latest",
            success: (res) => {
                sCallback(res)
                this._setlatestIndex(res.index)
                let key = this._getkey(res.index)
                wx.setStorageSync(key, res)
            }
        })
    }
    getClassic(index, nextorpre, sCallback) {
        let key = nextorpre == "next" ? this._getkey(index + 1) : this._getkey(index - 1)
        let classic = wx.getStorageSync(key)
        if (!classic) {
            this.request({ 
                url: `classic/${index}/${nextorpre}`,
                success: (res) => {
                    wx.setStorageSync(this._getkey(res.index), res)
                    sCallback(res)
                }
            })
        } else {
            sCallback(classic)
        }

    }
    getNext() {

    }


    isfirst(index) {
        return index === 1 ? true : false
    }

    islatest(index) {
        let latestindex = this._getlatestIndex();
        return latestindex == index ? true : false
    }
    _setlatestIndex(index) {
        wx.setStorageSync("latest", index)
    }
    _getlatestIndex() {
        let index = wx.getStorageSync('latest')
        return index
    }
    _getkey(index) {
        let key = "classic-" + index
        return key
    }

}
export {
    ClassicModel
}